package com.bac.bacplatform;

import android.content.Intent;
import android.net.http.SslError;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.CheckBox;
import com.android.volley.Response;
import com.android.volley.Response.Listener;
import com.bac.javabeans.Principal;
import com.bac.utils.Config;
import com.bac.utils.Util;
import com.google.gson.Gson;

/**
 * 加油宝
 * @author lerry
 *
 */
public class OilWalletActivity extends BaseActivity implements OnClickListener{
	private Button btnOpen;
	private WebView mTvAgreement;
	private CheckBox mCheckBox;

	private boolean mIslogin = false;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_oil_wallet);
		mTvAgreement = (WebView)findViewById(R.id.web_agree);
		mCheckBox = (CheckBox)findViewById(R.id.cb_agree);
		mTvAgreement.setWebViewClient(new BacWebClient());
		
		mTvAgreement.loadUrl(Config.URL_AGREEMENT);
		
		getCurrentuser();
	}

	@Override
	public void init() {
		btnOpen = (Button) findViewById(R.id.btn_open);
		btnOpen.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_open:
			if (mCheckBox.isChecked()) {
				if (mIslogin) {
					Intent intentToGasTreasure = new Intent(this,
							GasTreasureActivity.class);
					startActivity(intentToGasTreasure);
				}else{
					super.hanlderAutorError();
				}
			} else {
				showToast("请先同意接受以上协议");
			}
			break;
		default:
			break;
		}

	}
	
	public void getCurrentuser() {
		/**
		 * 成功返回结果
		 */
		Listener<String> sL = new Response.Listener<String>() {
			@Override
			public void onResponse(String response) {
				dismissLoadingProgress();
				mIslogin = true;
				Log.i("TAG----success", response.toString());
				
				Gson gson = new Gson();
				Principal principal = gson.fromJson(response, Principal.class);
				if (principal.isWealthAccountBanding()) {
					// 已开通
					
				}else{
					// 未开通
					
				}
				
			}
		};
		showLoadingProgress();
		
		String path = Config.URL_CURRENT_USER;
		Util.httpRequestToGet(path, sL, this);
	}
	
	private class BacWebClient extends WebViewClient{
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			view.loadUrl(url);
			return true;
		}

		@Override
		public void onReceivedSslError(WebView view, SslErrorHandler handler,
				SslError error) {
			handler.proceed();
		}

		@Override
		public void onReceivedError(WebView view, int errorCode,
				String description, String failingUrl) {
			super.onReceivedError(view, errorCode, description, failingUrl);
			// 网络异常
			Log.i("OilWalletActivity-------", "网络异常");
			//加载本地页面
			view.loadUrl("file:///android_asset/NetworkError.html");
			mCheckBox.setChecked(false);
			mCheckBox.setEnabled(false);
		}
	}

	@Override
	public void hanlderTimeoutError() {
		super.hanlderTimeoutError();
		mIslogin = false;
	}

	@Override
	public void hanlderErrorShowMessage(String string) {
		super.hanlderErrorShowMessage(string);
		mIslogin = false;
		showAlertDialog("提示", string);
	}

	@Override
	public void hanlderAutorError() {
//		super.hanlderAutorError();
		mIslogin = false;
		
	}

	@Override
	public void hanlderUnknowError() {
		super.hanlderUnknowError();
		mIslogin = false;
	}

	@Override
	public void hanlderNetworkError() {
		super.hanlderNetworkError();
		mIslogin = false;
	}
}
